<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Carousel</title>
  <style>
    body {
      background-size: cover;
      background-position: center;
      background-attachment: fixed;
      background-repeat: no-repeat;
    }

    .box {
      width: 60%;
      height: 0;
      padding-bottom: 40%;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      transition: 1.5s;
      border-radius: 10px;
    }

    .box:hover {
      box-shadow: 5px 5px 50px lightcoral;
      transform: translate(-50%, -50.5%);
    }

    .desc {
      width: 100%;
      height: 100%;
      position: absolute;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }

    .desc>p {
      text-align: center;
      margin: 0;
      padding: 5px;
      color: white;
      background-color: rgba(0, 0, 0, 0.24);
    }

    .desc>p:first-child {
      border-radius: 10px 10px 0 0;
    }

    .desc>p:last-child {
      border-radius: 0 0 10px 10px;
    }

    .imgBox>img {
      width: 100%;
      height: 100%;
      position: absolute;
      opacity: 0;
      transition: 1s;
      z-index: 0;
      border-radius: 10px;
    }

    .pointBox {
      position: absolute;
      right: 30px;
      bottom: 30px;
      display: flex;
    }

    .pointBox>div {
      z-index: 3;
      width: 10px;
      height: 10px;
      margin: 5px;
      background-color: white;
      border-radius: 50%;
      cursor: pointer;
    }

    .btnBox {
      width: 100%;
      height: 100%;
      position: absolute;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .btnBox>button {
      margin: 10px;
      width: 100px;
      height: 50px;
      outline: none;
      border: 1px solid black;
      background-color: greenyellow;
      border-radius: 10px;
      cursor: pointer;
      opacity: .3;
      transition: .3s;
    }

    .btnBox>button:hover {
      opacity: .8;
    }

    img.active {
      z-index: 1;
      opacity: 1;
    }
  </style>
</head>

<body style="background: url('./img/1.jpg');">
  <div class="box">
    <div class="imgBox">
      <img src="../img/1.jpg" alt="error occurs while loading!">
      <img src="../img/2.jpg" alt="error occurs while loading!">
      <img src="../img/3.jpg" alt="error occurs while loading!">
      <img src="../img/4.jpg" alt="error occurs while loading!">
      <img src="../img/5.jpg" alt="error occurs while loading!">
      <img src="../img/6.jpg" alt="error occurs while loading!">
      <img src="../img/7.jpg" alt="error occurs while loading!">
      <img src="../img/8.jpg" alt="error occurs while loading!">
      <img src="../img/9.jpg" alt="error occurs while loading!">
      <img src="../img/10.jpg" alt="error occurs while loading!">
    </div>
    <div class="desc">
      <p></p>
      <p></p>
    </div>
    <div class="pointBox">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>
    <div class="btnBox">
      <button>上一张</button>
      <button>下一张</button>
    </div>
  </div>
</body>
<script>
  const oBox = document.querySelector('.box')
  const oBody = document.querySelector('body')
  const oImg = document.querySelectorAll('img')
  const oPoint = document.querySelector('.pointBox').children
  const oBtn = document.querySelectorAll('button')
  const oDesc = document.querySelector('.desc').children
  const oDescTxt = [
    '冈山沿岸的发光海洋萤火虫',
    '德文特湖上的德文特岛房屋',
    '挪威默勒-鲁姆斯达尔郡群岛俯瞰图',
    '危地马拉的阿蒂特兰湖',
    '缅因州，阿卡迪亚国家公园中的约旦湖',
    '日落时的多伦多天际线',
    '阳光穿透加拉霍奈国家公园中的森林',
    '俄勒冈海岸日出时的麦克拱岩',
    '鸟瞰勃朗峰山脉中的南针峰',
    '杜德维卡塔拉大桥'
  ]
  const urlList = [
    'url("../img/1.jpg")',
    'url("../img/2.jpg")',
    'url("../img/3.jpg")',
    'url("../img/4.jpg")',
    'url("../img/5.jpg")',
    'url("../img/6.jpg")',
    'url("../img/7.jpg")',
    'url("../img/8.jpg")',
    'url("../img/9.jpg")',
    'url("../img/10.jpg")',
  ]
  const { length } = oImg
  let index = 0 // 显示对应下标的图片和改变对应下标的圆点的样式

  for (let i = 0; i < oPoint.length; i++) {
    oPoint[i].onclick = function () {
      index = i
      show()
    }
  }

  function show() {
    // 初始化样式
    for (let i = 0; i < length; i++) {
      oImg[i].style.opacity = 0
      oPoint[i].style.backgroundColor = 'white'
    }
    // 改变对应元素的样式
    oDesc[0].innerText = `${index + 1} / ${length}`
    oDesc[1].innerText = `${oDescTxt[index]}`
    oBody.style.background = urlList[index]
    oImg[index].style.opacity = 1
    oPoint[index].style.backgroundColor = 'lawngreen'
  }
  show()
  // 下一张图片，要考虑显示最后一张图片的时候重置
  function next() {
    index = (index == length - 1) ? 0 : index + 1
    show()
  }
  // 上一张图片，要考虑显示第一张图片的时候重置
  function last() {
    index = (index == 0) ? length - 1 : index - 1
    show()
  }
  // 左右两个按钮的点击事件
  oBtn[0].onclick = last
  oBtn[1].onclick = next

  // 通过定时器让图片自动切换
  var timer = setInterval(next, 3000)
  // 鼠标移入暂停定时器
  oBox.onmouseover = function () {
    clearInterval(timer)
  }
  // 鼠标移出继续自动切换
  oBox.onmouseout = function () {
    timer = setInterval(next, 3000)
  }
</script>

</html>